package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.admin.MarketAdminService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {

    @Autowired
    MarketAdminService marketAdminAdminService;

    // page=1&limit=20&username=2&sort=add_time&order=desc
    @GetMapping("list")
    public Object list(Integer page, Integer limit, String username, String sort, String order) {

        ArrayList<Object> list = new ArrayList<>();
        List<MarketAdmin> adminList = marketAdminAdminService.list(page, limit, username, sort, order);

        for (MarketAdmin marketAdmin : adminList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", marketAdmin.getId());
            map.put("username", marketAdmin.getUsername());
            map.put("avatar", marketAdmin.getAvatar());
            map.put("roleIds", marketAdmin.getRoleIds());
            list.add(map);
        }
        return ResponseUtil.okList(list);
    }


    @PostMapping("update")
    public Object update(@RequestBody Map<String,Object> map) {

        Integer id = (Integer) map.get("id");
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String avatar = (String) map.get("avatar");
        Integer[] roleIds = (Integer[]) map.get("roleIds");

        int row = marketAdminAdminService.update(id, username, avatar, roleIds, password);

        if (username.length() < 6) {
            return ResponseUtil.fail(601, "管理员名称不符合规定");
        } else if (password.length() < 6) {
            return ResponseUtil.fail(602, "管理员密码长度不得少于6");
        }
        if (row == 1) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    //row==1修改成功
    //传参数,新对象,条件
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String,Integer> map) {

        Integer id = map.get("id");
        int row = marketAdminAdminService.delete(id);

        if (row == 1) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("create")
    public Object create(@RequestBody Map<String,Object> map) {

        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String avatar = (String) map.get("avatar");
        Integer[] roleIds = (Integer[]) map.get("roleIds");

        //查已有数据
        List<MarketAdmin> adminList = marketAdminAdminService.listByName(username);
        for (MarketAdmin marketAdmin : adminList) {
            //用户名相等返回602
            if ((marketAdmin.getUsername()).equals(username)) {
                return ResponseUtil.fail(602, "管理员已存在");
            }
        }
        if (username.length() < 6) {
            return ResponseUtil.fail(601, "管理员名称不符合规定");
        } else if (password.length() < 6) {
            return ResponseUtil.fail(602, "管理员密码长度不得少于6");
        }
        //得到一个新增成员列表
        List<MarketAdmin> adminCreates = marketAdminAdminService.create(password, avatar, roleIds, username);

        HashMap<String, Object> result = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        for (MarketAdmin adminCreate : adminCreates) {
            result.put("addTime", adminCreate.getAddTime());
            result.put("avatar", adminCreate.getAvatar());
            result.put("id", adminCreate.getId());
            result.put("password", adminCreate.getPassword());
            result.put("roleIds", adminCreate.getRoleIds());
            result.put("updateTime", adminCreate.getUpdateTime());
            result.put("username", adminCreate.getUsername());
            list.add(result);
        }
        return ResponseUtil.okList(list);

    }


}
